package com.groupseven.backend.controller;

import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.Role.Role;
import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.domain.User.UserRole;
import com.groupseven.backend.services.RoleService;
import com.groupseven.backend.test.DataEncode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private DataEncode dataEncode;

    @GetMapping("/search/{query}")
    public ResultType FuzzySearch(@PathVariable String query){
        ResultType result = new ResultType(1,"搜索成功！",200);
        result.setRoles(roleService.FuzzySearch(query));
        return result;
    }

    @GetMapping("")
    public ResultType getAllRoles(){
        ResultType result = new ResultType(1,"查询角色成功！",200);
        result.setRoles(roleService.getAllRoles());
        return result;
    }

    @PostMapping("")
    public ResultType addRole(@RequestBody Role role){
        Subject subject= SecurityUtils.getSubject();
        User currentUser=(User)subject.getPrincipal();
        currentUser.setUsername(dataEncode.decodeBae64(currentUser.getUsername()));
        role.setCreator(currentUser.getUsername());
        String nowtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        Date date = DateUtil.parse(nowtime);
        role.setCreatedDate(date);
        List<Role> roles = roleService.getAllRoles();
        for(Role Iterator : roles){
            if(Iterator.getRolename().equals(role.getRolename())){
                return new ResultType(0,"重复插入！",200);
            }
        }
        if(roleService.addRole(role)==1){
            return new ResultType(1,"添加角色成功！",200);
        }
        return new ResultType(0,"添加角色失败！",200);
    }

    @PutMapping("/userrole")
    public ResultType updateUserRole(@RequestBody UserRole ur){
        String Username = ur.getUsername();
        int Rolenumber = ur.getUsertype();
        String str = dataEncode.encodeBae64(Username);
        if(roleService.updateRoleByUsernameAndRolenumber(str,Rolenumber)==1){
            return new ResultType(1,"修改"+Username+"的角色成功！",200);
        }
        return new ResultType(0,"修改"+Username+"的角色失败！",200);
    }

    @DeleteMapping("/{Rolenumber}")
    public ResultType deleteRoleByRolenumber(@PathVariable int Rolenumber){
        List<Role> roles = roleService.getAllRoles();
        boolean flag = false;
        for(Role Iterator : roles){
            if(Iterator.getRolenumber()==Rolenumber){
                flag = true;
            }
        }
        if(flag&&roleService.deleteRoleByRolenumber(Rolenumber)==1){
            return new ResultType(1,"删除角色成功！",200);
        }
        return new ResultType(0,"删除角色失败！",200);
    }

    @PutMapping("")
    public ResultType updateRole(@RequestBody Role role){
        List<Role> roles = roleService.getAllRoles();
        boolean flag = false;
        for(Role Iterator : roles){
            if(Iterator.getRolenumber()==role.getRolenumber()){
                flag = true;
            }
        }
        if(flag && roleService.updateRole(role)==1){
            return new ResultType(1,"修改角色信息成功！",200);
        }
        return new ResultType(0,"修改角色信息失败！",200);
    }

    @PostMapping("/s/")
    public ResultType multi_delete(@RequestBody List<Role> roles){
        if(roleService.multi_delete(roles)){
            return new ResultType(1,"批量删除成功！",200);
        }
        return new ResultType(0,"批量删除失败！",200);
    }
}
